package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 搜索页面渲染
 */
@Controller
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;


    //开始搜索
    @GetMapping("/search.html")
    public String search(SearchParam searchParam, Model model) throws Exception{
        //1: 品牌集合 平台集合 商品集合 分页相关
        Result<Map> result = listFeignClient.search(searchParam);// SearchResponseVo
        model.addAllAttributes(result.getData());//远程调用 搜索微服务提供 四部分
        //2:关键词回显
        model.addAttribute("searchParam",searchParam);
        //3:选中项  回显 品牌
        String trademarkParam = makeTradeParam(searchParam);
        model.addAttribute("trademarkParam",trademarkParam);
        //4:选中项 回显 平台属性
        List<Map> propsParamList =  makePropsParamList(searchParam);
        model.addAttribute("propsParamList",propsParamList);
        //5:urlParam  请求路径？大量参数    隐藏
        String urlParam = makeUrlParam(searchParam);
        model.addAttribute("urlParam",urlParam);
        //6:排序 回显
        Map orderMap = makeOrderMap(searchParam);
        model.addAttribute("orderMap",orderMap);
        //搜索页面
        return "list/index";
    }

    //排序 回显
    private Map makeOrderMap(SearchParam searchParam) {
        Map orderMap = new HashMap();
        String order = searchParam.getOrder();
        if(!StringUtils.isEmpty(order)){
            //order=1:desc   或是 1:asc  或是 2:desc 或是2:asc
            String[] o = StringUtils.split(order, ":");
            orderMap.put("type",o[0]);
            orderMap.put("sort",o[1]);

        }else{
            //为“”  综合 降序
            orderMap.put("type","1");
            orderMap.put("sort","desc");
        }
        return orderMap;

    }

    //请求路径？大量参数  回显
    private String makeUrlParam(SearchParam searchParam) {
        //字符串容器
        StringBuilder urlParam = new StringBuilder();
        urlParam.append("/search.html?");
        //判断关键词
        String keyword = searchParam.getKeyword();
        if(!StringUtils.isEmpty(keyword)){
            urlParam.append("keyword=").append(keyword);
        }
        //判断一二三级分类
        Long category1Id = searchParam.getCategory1Id();
        Long category2Id = searchParam.getCategory2Id();
        Long category3Id = searchParam.getCategory3Id();
        if(null != category1Id){
            urlParam.append("&category1Id=").append(category1Id);
        }
        if(null != category2Id){
            urlParam.append("&category2Id=").append(category2Id);
        }
        if(null != category3Id){
            urlParam.append("&category3Id=").append(category3Id);
        }
        //品牌
        String trademark = searchParam.getTrademark();
        if(!StringUtils.isEmpty(trademark)){
            urlParam.append("&trademark=").append(trademark);
        }
        //平台属性
        String[] props = searchParam.getProps();
        if(null != props && props.length > 0){
            for (String prop : props) {
                urlParam.append("&props=").append(prop);
            }
        }
        return urlParam.toString();
    }

    //回显 平台属性
    private List<Map> makePropsParamList(SearchParam searchParam) {
        List<Map> listMap = new ArrayList<>();
        String[] props = searchParam.getProps();
        if(null != props && props.length > 0){
            for (String prop : props) {
                String[] p = prop.split(":");
                // 0 属性ID
                // 1 属性值名称
                // 2 属性名称
                Map map = new HashMap();
                map.put("attrName",p[2]);
                map.put("attrValue",p[1]);
                map.put("attrId",p[0]);//修改BUG   平台属性取消按钮回显问题  丢失ID
                listMap.add(map);
            }
        }
        return listMap;

    }

    //回显品牌
    private String makeTradeParam(SearchParam searchParam) {
        String result = "";
        //判断品牌是否作为入参传递进来
        String trademark = searchParam.getTrademark();// trademark  = 2:华为
        if(!StringUtils.isEmpty(trademark)){
            String[] t = trademark.split(":");//
            result = "品牌:" + t[1];
        }
        return result;
    }
}
